/*
 *Copyright (c) 2024 Black Sesame Technologies
 *
 *Licensed under the Apache License, Version 2.0 (the "License");
 *you may not use this file except in compliance with the License.
 *You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 *Unless required by applicable law or agreed to in writing, software
 *distributed under the License is distributed on an "AS IS" BASIS,
 *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *See the License for the specific language governing permissions and
 *limitations under the License.
*/

#include <string.h>
#include "printf_inc.h"
#include "sys_base_reg.h"
#include "C1200_Flexcan.h"
#include "FlexCAN_Ip.h"
#include "VCan_Types.h"
#include "can_output.h"


// 用于记录每个controller的状态，Bitmap
static uint32 can_controller_status_bitmap = 0;
// 记录每个Controller的NEXIF寄存器
static uint8 can_controller_NEXIF[LLCG_CONFIG_MAXCTRL_COUNT] = {0};
// 记录每个Controller的NFE寄存器
static uint8 can_controller_NFE[LLCG_CONFIG_MAXCTRL_COUNT] = {0};

/**
 * @brief 转换id_hit参数为第N个filter
 *        参考flexcan_ug_2.0.1.pdf, id_hit的值和ERFCF寄存器有关
 *        如果是扩展帧, id_hit的值等于 CAN_ERFFELx寄存器的编号，由于每两个 CAN_ERFFEL寄存器用于匹配一个扩展帧，
 *                     所以最终值为 (id_hit * 2)
 *        如果是标准帧，id_hit的值等于 CAN_ERFFELx寄存器的编号 - NFXIF。 
 *                     所以最终值为 id_hit + NFXIF*2
*/
uint32 can_hit_id_to_filter_id(uint32 controller_id, bool ext_frame, uint32 hit_id)
{
    uint8 NFXIF;

    if (controller_id >= LLCG_CONFIG_MAXCTRL_COUNT) {
        return 0;
    }

    NFXIF = can_controller_NEXIF[controller_id];
    return ext_frame ? (hit_id * 2) : (hit_id + NFXIF * 2);
}

/**
 * @brief: 将id_hit_filter_index转换为hwobject id
 *         转换规则如下： 如果是扩展帧，hwobject id = (id_hit_filter_index / 2)
 *                       如果是标准帧，hwobject id = (id_hit_filter_index - NEXIF*2)/2 + NEXIF
*/
uint32 can_filter_id_to_hwobject_id(uint32 controller_id, bool ext_frame, uint32 filter_id)
{
    uint8 NFXIF;

    if (controller_id >= LLCG_CONFIG_MAXCTRL_COUNT) {
        return 0;
    }

    NFXIF = can_controller_NEXIF[controller_id];
    return ext_frame ? (filter_id / 2) : (((filter_id - NFXIF * 2) / 2) + NFXIF);
}

uint32 can_is_forward_port(uint32 controller_id)
{
    return can_controller_status_bitmap & (1 << controller_id);
}

void can_forward_port_init(uint32 controller_id)
{
    uint32 ERFCF_reg_vaule;
    uint8 NEXIF;
    uint8 NFE;

    if (controller_id >= LLCG_CONFIG_MAXCTRL_COUNT) {
        return;
    }

    ERFCF_reg_vaule = Flexcan_Ip_apxBase[controller_id]->ERFCR;
    NEXIF = ((ERFCF_reg_vaule &FLEXCAN_ERFCR_NEXIF_MASK) >> FLEXCAN_ERFCR_NEXIF_SHIFT);
    NFE = ((ERFCF_reg_vaule &FLEXCAN_ERFCR_NFE_MASK) >> FLEXCAN_ERFCR_NFE_SHIFT);

    can_controller_NEXIF[controller_id] = NEXIF;
    can_controller_NFE[controller_id] = NFE;
    can_controller_status_bitmap |= 1 << controller_id;

    can_que_mb_addr_init(controller_id);
    hprintf(DEBUG, "can forward port:%u ERFCF_reg_vaule:0x%x NEXIF:%d NFE:%d\n",
        controller_id, ERFCF_reg_vaule, NEXIF, NFE);
}
